import java.awt.Point;
import java.util.Iterator;
import java.util.LinkedList;


public class Snake implements Iterable<Point>{
	
	private LinkedList<Point> chain;
	private int eaten;

		
	public Snake(int x, int y){
		chain = new LinkedList<Point>();
		chain.addFirst(new Point(x,y));
		eaten = 0;
	}

	public Snake(Point h){
		chain = new LinkedList<Point>();
		chain.addFirst(h);
		eaten = 0;
	}
	
	private void turn(int dx, int dy) {
		
		Point head = chain.getFirst();
		
		Point newHead = (Point) head.clone(); 
		newHead.translate(dx, dy);
		chain.addFirst(newHead);

		if(eaten==0){
			chain.removeLast();
		}else{
			eaten = eaten-1;
		}
	}
	
	public void turnUp(){
		turn(0,-1);		
	}
	public void turnRight(){
		turn(1,0);
	}
	public void turnDown(){
		turn(0,1);
	}
	public void turnLeft(){
		turn(-1,0);
	}
	public void jump(Point p){
		chain.addFirst(p);
		
		if(eaten==0){
			chain.removeLast();
		}else{
			eaten = eaten-1;
		}
	}
	
	public void eat(int q){
			if(q>=0)
				eaten = eaten + q;
	}
	public int size(){
		return ( chain.size() + eaten ) -1;
	}

	public Iterator<Point> iterator() {
		return chain.iterator();
	}
	
	public Point getHeadLocation(){
		return (Point) chain.getFirst().clone(); 
	}
	
	public boolean isOver(Point p){
		
		for (Point trunk : chain) {
			if(trunk.equals(p))
				return true;
		}
		return false;
	}
	public boolean autoBeaten(){
		Iterator<Point> it = chain.iterator();
		Point head = it.next();
		while(it.hasNext()){
			if(it.next().equals(head))
				return true;
		}
		return false;
	}

}
